//
//  HandPartyMoneyViewController.m
//  ShangHaiHuDongApp
//
//  Created by 刘应平 on 2017/6/15.
//  Copyright © 2017年 刘应平. All rights reserved.
//

#import "HandPartyMoneyViewController.h"
#import <JavaScriptCore/JavaScriptCore.h>
@interface HandPartyMoneyViewController ()<UIWebViewDelegate>
/** webView */
@property (nonatomic, strong) UIWebView *webView;
@end

@implementation HandPartyMoneyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titlelabel.text = self.titleString;
    [self.view addSubview:self.webView];
}



- (void)action_leftButton:(UIButton *)sender{
    
     [self.webView stringByEvaluatingJavaScriptFromString:@"window.disposeWebView()"];

}
#pragma mark - UIWebView 协议方法
- (void)webViewDidFinishLoad:(UIWebView *)webView{
    
    [YJProgressHUD hide];
   
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{

        __weak typeof(self) weakSelf = self;
        /**  直接关闭方法  */
        [webView stringByEvaluatingJavaScriptFromString:@"window.exitIosPayCurrentPage()"];
        /**  返回上一页方法  */
        [webView stringByEvaluatingJavaScriptFromString:@"window.goBackIosPayPage()"];
        
        
        //其中 openImage 是JS的函数名，得到的 arg数组 里面为JS的 passValue 函数的参数，即 JS要传给OC的参数。
        JSContext *context = [webView valueForKeyPath:@"documentView.webView.mainFrame.javaScriptContext"];

        
        context[@"goBackIosPayPage"] = ^{
            NSArray *infor = [JSContext currentArguments];
            JSValue *imageString = infor.firstObject;
            NSString *title = [imageString toString];
            if ([title isEqualToString:@"IOS"]) {
                if ([weakSelf.webView canGoBack]) {
                    [weakSelf.webView goBack];
                }else{
                    
                    [weakSelf.navigationController popToRootViewControllerAnimated:YES];
                    
                }
            }
            
        };
        
        
        
        context[@"exitIosPayCurrentPage"] = ^{
            NSArray *infor = [JSContext currentArguments];
            JSValue *imageString = infor.firstObject;
            NSString *title = [imageString toString];
            if ([title isEqualToString:@"IOS"]) {
                [weakSelf.navigationController popToRootViewControllerAnimated:YES];
            }
            
                
            
        };
    });
    
    
}


- (void)webViewDidStartLoad:(UIWebView *)webView{
    
    //    [YJProgressHUD hide];
}
#pragma mark - Setter


- (void)setUrlString:(NSString *)urlString{
    _urlString = urlString;
    NSURLRequest *webRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]
                                                cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                            timeoutInterval:30];
    [self.webView loadRequest:webRequest];
    [YJProgressHUD showMessage:@"加载中..." duration:10 view:self.view];
}

#pragma mark - Getters

- (UIWebView *)webView{
    if (_webView == nil){
        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, LP_TopHeight, [UIScreen zj_screenWith], [UIScreen zj_screenHeight] - LP_TopHeight )];
        _webView.delegate = self;
        _webView.dataDetectorTypes = UIDataDetectorTypePhoneNumber | UIDataDetectorTypeLink | UIDataDetectorTypeShipmentTrackingNumber | UIDataDetectorTypeFlightNumber | UIDataDetectorTypeLookupSuggestion;
        _webView.backgroundColor = [UIColor clearColor];
        [_webView setOpaque:YES];
    }
    return _webView;
}


@end
